import {
  UserHttp
} from '../../../api/user.js'

const userModel = new UserHttp();

Page({
  initFlag: false,
  data: {
    listPage: [], // 流水列表
    isShowTips: false, // 是否显示余额迁移提示
    kryBlance: 0, // 客如云余额
    serviceText:false,
    userInfo: {
      common: {
        pea_rate: 10000,
        pea_blance: 0
      }
    }
  },

  initPage() {
    this.initData();
  },

  onPullDownRefresh: function() {
    this.initData(true)
  },

  // 充值
  onCharge() {
    // wx.navigateTo({
    //   url: '/pages/me/charge/index'
    // })
  },

  // 提现
  onWithdraw() {
    wx.navigateTo({
      url: '/pages/me/balance/withdrawal/index'
    })
  },

  onHide() {
    this.initFlag = true;
  },

  onShow() {
    if (this.initFlag === true) {
      this.initFlag = false;
      this.initData();
    }
  },

  initData: function() {
    this.httpGetUserInfo();
    this.httpGetKryBlance();
    this.httpGetMemberBalanceBill()
  },

  // 点击咨询按钮
  onTransfer() {
    wx.navigateTo({
      url: `/pages/me/transfer/transfer?kryBlance=${this.data.kryBlance}`
    });
  },

  // 获取用户信息
  httpGetUserInfo () {
    return userModel.getUserInfo().then((res) => {
      this.setData({
        userInfo: res,
      })
    })
  },

  // 获取客如云平台的用户余额 - 用于判断是否显示迁移提示
  httpGetKryBlance() {
    return userModel.getKryBlance({}).then((res) => {
      if (res.blance > 0) {
        this.setData({
          kryBlance: res.blance,
          isShowTips: true
        })
      }
    })
  },

  // 获取会员余额账单
  httpGetMemberBalanceBill () {
    return userModel.getMemberBalanceBill({
      page: 1,
      limit: 10
    }).then((res) => {
      wx.stopPullDownRefresh();
      this.setData({
        listPage: res.list
      })
    })
  }
});
